package io.github.doc.ui;

import io.github.doc.config.PluginSetting;
import io.github.doc.config.state.PluginSettingsState;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import javax.swing.JComponent;

/**
 * <p>Company: 深圳振瀚信息技术有限公司成都分公司 </p>
 * <p>Description: </p>
 *
 * @param <V> parameter
 * @param <J> parameter
 * @author ZH.Team
 * @version 1.0.0
 * @email "mailto:ZH.Team@zhxx.com"
 * @date 2025.01.04 14:26
 * @since y.y.y
 */
@Data
@RequiredArgsConstructor
public abstract class AbstractComponentWrapper<V, J extends JComponent> implements Wrapper<V, J> {

    /**
     * Title
     */
    private final String title;

    /**
     * Component
     */
    private final J component;

    /**
     * Key
     */
    private final String key;

    /**
     * Default value
     */
    private V defaultValue;

    /**
     * Reset
     *
     * @since y.y.y
     */
    @Override
    public void reset() {
        PluginSettingsState pluginSettingsState = PluginSettingsState.getInstance();
        PluginSetting state = pluginSettingsState.getState();
        this.load(state);
    }

    /**
     * Save
     *
     * @since y.y.y
     */
    public void doSave(V v) {
        PluginSettingsState pluginSettingsState = PluginSettingsState.getInstance();
        PluginSetting state = pluginSettingsState.getState();
        if (state == null) {
            return;
        }
        state.addConfig(this.key, v.toString());
    }
}
